[id].tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { Head } from "fresh/runtime";
  2. import { HttpError } from "fresh";
  3. import { page, type PageProps } from "fresh";
  4. import { define } from "utils/state.ts";
  5. import { checkToken } from "utils/server.ts";
  6. import { find } from "utils/db.ts";
  7. import TopBar from "../islands/TopBar.tsx";
  8. import Editor, { EditorMode } from "../islands/Editor.tsx";
  9. import PageContainer from "../components/layout/PageContainer.tsx";
  10. interface PostProps {
  11. id: string;
  12. title: string;
  13. content: string;
  14. shared: boolean;
  15. isLogined: boolean;
  16. allowMode: EditorMode;
  17. }
  18. export const handler = define.handlers({
  19. GET(ctx) {
  20. const tokenUserId = checkToken(ctx.req);
  21. const postId = ctx.params.id;
  22. const post = find(
  23. "Post",
  24. tokenUserId
  25. ? { id: postId, user_id: tokenUserId }
  26. : { id: postId, shared: 1 },
  27. ["title", "content", "shared"],
  28. );
  29. if (post.length > 0) {
  30. return page({
  31. id: postId,
  32. isLogined: Boolean(tokenUserId),
  33. allowMode: tokenUserId ? EditorMode.Both : EditorMode.Read,
  34. title: post[0]["title"] as string,
  35. content: post[0]["content"] as string,
  36. shared: post[0]["shared"] === 1,
  37. });
  38. }
  39. throw new HttpError(404);
  40. },
  41. });
  42. export default function Post(props: PageProps<PostProps>) {
  43. return (
  44. <>
  45. <Head>
  46. <title>{props.data.title}</title>
  47. </Head>
  48. <PageContainer>
  49. <TopBar
  50. id={props.data.id}
  51. title={props.data.title}
  52. shared={props.data.shared}
  53. allowMode={props.data.allowMode}
  54. isLogined={props.data.isLogined}
  55. />
  56. <Editor
  57. id={props.data.id}
  58. content={props.data.content}
  59. allowMode={props.data.allowMode}
  60. />
  61. </PageContainer>
  62. </>
  63. );
  64. }